package com.zzx.test.treeset;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.CollationKey;
import java.text.Collator;

/**
 * Person类 有属性  name,age,sex
 * 排序规则: 第一条件  年龄降序,第二条件  姓名 降序,第三条件 性别升序
 *
 * @author Administrator
 */
@Data
@NoArgsConstructor
public class Person implements Comparable<Person> {
    private String name;
    private int age;
    private String sex;

    public Person(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    //排序规则: 第一条件  年龄降序,第二条件  姓名 降序,第三条件 性别升序
    @Override
    public int compareTo(Person o) {
        if (age > o.age) {
            return 1;
        } else if (age < o.age) {
            return -1;
        } else {
            CollationKey key1 = Collator.getInstance().getCollationKey(name);
            CollationKey key2 = Collator.getInstance().getCollationKey(o.name);
            int num = key1.compareTo(key2);
            if (num > 0) {
                return 1;
            } else if (num < 0) {
                return -1;
            } else {
                CollationKey key3 = Collator.getInstance().getCollationKey(sex);
                CollationKey key4 = Collator.getInstance().getCollationKey(o.sex);
                int s = key3.compareTo(key4);
                return Integer.compare(s, 0);
            }
        }
    }
}
